package com.maxjay.system.web.controller.struts;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.maxjay.system.bo.UserInfo;
import com.maxjay.system.po.TSysUser;
import com.maxjay.system.service.UserService;

@Scope("prototype")
@Component("loginController")
public class LoginController extends BaseController {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private TSysUser user;
	
	private String errorMsg;
	
	@Resource
	private UserService userService;
	
	public String execute() {
		HttpServletRequest request = ServletActionContext.getRequest();
		TSysUser validateUser = userService.validateUser(user.getLoginId(), user.getPassword());
		if (validateUser != null) {
			UserInfo sessionUser = userService.initUserInfo(validateUser);
			this.setCurrentSessionUser(sessionUser);
			Map<String, String> backUrlMap = new HashMap<String, String>();
			request.getSession().setAttribute("backUrlMap", backUrlMap);
			return SUCCESS;
		} else {
			this.addActionError("用户名或密码错误");
			return "failure";
		}
	}

	public TSysUser getUser() {
		return user;
	}

	public void setUser(TSysUser user) {
		this.user = user;
	}

	public String getErrorMsg() {
		return errorMsg;
	}

	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}

}
